package com.tom.controller;

import com.tom.common.MyOrder;
import com.tom.common.OrderItem;
import com.tom.pojo.Comment;
import com.tom.pojo.HeiZaiOrder;
import com.tom.service.IOrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    private Map<String, Object> responseMsg = new HashMap<>();

    @Resource
    private IOrderService orderService;

    /**
     * 新建订单
     * @param orderItem 订单数据
     * @return 新建结果
     */
    @RequestMapping("/neworder")
    @ResponseBody
    public Map<String, Object> newOrder(@RequestBody OrderItem orderItem) {
        responseMsg.clear();
        int result = orderService.newOrder(orderItem);

        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "order success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "order failure");
        }
        return responseMsg;
    }

    @RequestMapping("/myorder")
    @ResponseBody
    public Map<String, Object> myOrder(@RequestParam Integer uid) {
        responseMsg.clear();

        List<MyOrder> myOrderList = orderService.myOrder(uid);
        if (!myOrderList.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get my order list success");
            responseMsg.put("my_order_list", myOrderList);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get my order list failure");
        }
        return responseMsg;
    }

    @RequestMapping("/latestorder")
    @ResponseBody
    public Map<String, Object> latestOrder(@RequestParam Integer rid) {
        responseMsg.clear();

        List<MyOrder> latestOrderList = orderService.latestOrder(rid);
        if (!latestOrderList.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get latest order list success");
            responseMsg.put("latest_order_list", latestOrderList);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get latest order list failure");
        }
        return responseMsg;
    }

    @RequestMapping("/completed")
    @ResponseBody
    public Map<String, Object> completedOrder(@RequestParam Integer rid) {
        responseMsg.clear();

        List<MyOrder> completedOrderList = orderService.completedOrder(rid);
        if (!completedOrderList.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get completed order list success");
            responseMsg.put("completed_order_list", completedOrderList);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get completed order list failure");
        }
        return responseMsg;
    }

    @RequestMapping("/receive")
    @ResponseBody
    public Map<String, Object> receiveOrder(@RequestParam Integer id) {
        responseMsg.clear();
        int result = orderService.receiveOrder(id);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "receive order success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "receive order failure");
        }
        return responseMsg;
    }

    @RequestMapping("/send")
    @ResponseBody
    public Map<String, Object> orderSend(@RequestParam Integer id) {
        responseMsg.clear();
        int result = orderService.send(id);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "send success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "send failure");
        }
        return responseMsg;
    }

    @RequestMapping("/confirm")
    @ResponseBody
    public Map<String, Object> orderConfirm(@RequestParam Integer id) {
        responseMsg.clear();
        int result = orderService.confirm(id);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "order confirm success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "order confirm failure");
        }
        return responseMsg;
    }

    @RequestMapping("/comment")
    @ResponseBody
    public Map<String, Object> orderComment(@RequestBody Comment comment) {
        responseMsg.clear();
        comment.setAddTime(new Date());
        int result = orderService.comment(comment);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "comment success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "comment failure");
        }

        return responseMsg;
    }

    @RequestMapping("/mycomment")
    @ResponseBody
    public Map<String, Object> myComment(@RequestParam Integer id) {
        responseMsg.clear();
        Comment comment = orderService.myComment(id);
        if (comment!=null) {
            responseMsg.put("success", true);
            responseMsg.put("message", "show this comment success");
            responseMsg.put("rate", comment.getRate());
            responseMsg.put("content", comment.getContent());
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "comment failure");
        }

        return responseMsg;
    }
}
